package com.liutaw.nanjingair.presenter.products;

import com.liutaw.nanjingair.views.activities.AndroidApplication;
import com.liutaw.nanjingair.views.activities.products.IWeatherForcastView;
import com.szjlxh.data.entity.WeatherPerSixHourBean;
import com.szjlxh.data.entity.WeatherSevenDayBean;
import com.szjlxh.data.entity.WeatherThreeDayForcastBean;
import com.szjlxh.data.repository.WeatherForcastRespository;
import com.szjlxh.domain.model.WeatherPerHour;
import com.szjlxh.domain.model.index.WeatherSevenDay;

import java.util.List;

import javax.inject.Inject;

import rx.Observer;

/**
 * Created by liutao on 16/3/19.
 */
public class WeatherPresenter implements IWeatherPresenter{

    private IWeatherForcastView iWeatherForcastView;
    private WeatherForcastRespository respository;

    @Inject
    public WeatherPresenter() {

        respository =new WeatherForcastRespository();
    }

    @Override
    public void requestLocation() {

    }

    @Override
    public void requestPerHour() {
        respository.weatherPerHour().subscribe(new Observer<List<WeatherPerHour>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                AndroidApplication.getInstance().showToastMessage(e.getMessage());
            }

            @Override
            public void onNext(List<WeatherPerHour> weatherPerHours) {
                iWeatherForcastView.onPerHourLoaded(weatherPerHours);
            }
        });
    }

    @Override
    public void reqeustPerThress() {
        respository.weatherPerThreeHour().subscribe(new Observer<List<WeatherPerHour>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                AndroidApplication.getInstance().showToastMessage(e.getMessage());
            }

            @Override
            public void onNext(List<WeatherPerHour> weatherPerHours) {
                iWeatherForcastView.onPerThreeLoaded(weatherPerHours);
            }
        });
    }

    @Override
    public void requestPerSix() {
        respository.weatherPerSixHour().subscribe(new Observer<List<WeatherPerSixHourBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                AndroidApplication.getInstance().showToastMessage(e.getMessage());
            }

            @Override
            public void onNext(List<WeatherPerSixHourBean> weatherPerSixHourBeen) {
                iWeatherForcastView.onPerSixLoaded(weatherPerSixHourBeen);
            }
        });
    }

    @Override
    public void requestSevenDay() {
        respository.weatherSevenDay().subscribe(new Observer<List<WeatherSevenDay>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                AndroidApplication.getInstance().showToastMessage(e.getMessage());
            }

            @Override
            public void onNext(List<WeatherSevenDay> data) {
                iWeatherForcastView.onSevenDayLoaded(data);
            }
        });
    }

    @Override
    public void requestThreeDayForcast() {
        respository.weatherThreeDayForcast().subscribe(new Observer<WeatherThreeDayForcastBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                AndroidApplication.getInstance().showToastMessage(e.getMessage());
            }

            @Override
            public void onNext(WeatherThreeDayForcastBean weatherThreeDayForcastBean) {
                iWeatherForcastView.onThreeDayForcastLoaded(weatherThreeDayForcastBean);
            }
        });
    }

    public void setiWeatherForcastView(IWeatherForcastView iWeatherForcastView) {
        this.iWeatherForcastView = iWeatherForcastView;
    }
}
